package com.simon.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simon.train.business.domain.Station;
import com.simon.train.business.domain.StationExample;
import com.simon.train.business.dto.StationQueryDto;
import com.simon.train.business.dto.StationSaveDto;
import com.simon.train.business.mapper.StationMapper;
import com.simon.train.business.vo.StationQueryVO;
import com.simon.train.common.exception.BusinessException;
import com.simon.train.common.exception.BusinessExceptionEnum;
import com.simon.train.common.util.SnowUtil;
import com.simon.train.common.vo.PageVO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StationService {

    private static final Logger LOG = LoggerFactory.getLogger(StationService.class);

    @Resource
    private StationMapper stationMapper;

    public void SaveStation(StationSaveDto stationSaveDto){
        Station station = BeanUtil.copyProperties(stationSaveDto, Station.class);
        /**
         * 对于首次保存 也就是新增 需要对字段单独做处理
         * 主要用于区分到底是 新增 还是现有的 编辑
         */
        if (ObjectUtil.isNull(station.getId())){
            /**
             * 保存之前 需要先校验唯一键是否存在 避免出现名字冲突问题
             * 主要针对新增问题 去校验该站名是否已经存在了 存在的话不能重复创建
             *
             * StationExample stationExample = new StationExample();
             * //写法1
             * //            StationExample.Criteria criteria = stationExample.createCriteria();
             * //            criteria.andNameEqualTo(stationSaveDto.getName());
             * //写法2
             * stationExample.createCriteria().andNameEqualTo(stationSaveDto.getName());
             * List<Station> stations = stationMapper.selectByExample(stationExample);
             */
            Station stationDB = SelectByUnique(stationSaveDto.getName());

            if(ObjectUtil.isNotEmpty(stationDB)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_STATION_NAME_UNIQUE_ERROR);
            }
            station.setId(SnowUtil.getSnowflakeNextId());
            DateTime time = DateTime.now();
            station.setCreateTime(time);
            station.setUpdateTime(time);
            stationMapper.insert(station);


        } else {
            station.setUpdateTime(DateTime.now());
            stationMapper.updateByPrimaryKey(station);
        }

    }

    /**
     * 将这个通过查询唯一键是否为空的方法给提取出来
     * 便于后续针对所有的唯一键的查询进行调用
     * @param name
     * @return
     */
    private Station SelectByUnique(String name) {
        StationExample stationExample = new StationExample();
        stationExample.createCriteria().andNameEqualTo(name);
        List<Station> list = stationMapper.selectByExample(stationExample);
        if(CollUtil.isNotEmpty(list)){
            return list.get(0);
        }else return null;
    }

    public PageVO<StationQueryVO> QueryStationList(StationQueryDto stationQueryDto){
        StationExample stationExample = new StationExample();
        stationExample.setOrderByClause("id desc");
        StationExample.Criteria criteria = stationExample.createCriteria();
        LOG.info("当前请求的页码：{}", stationQueryDto.getPage());
        LOG.info("每页条数：{}", stationQueryDto.getPageSize());

        PageHelper.startPage(stationQueryDto.getPage(),stationQueryDto.getPageSize());
        List<Station> stationList = stationMapper.selectByExample(stationExample);

        PageInfo<Station> pageInfo = new PageInfo<>(stationList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<StationQueryVO> lists = BeanUtil.copyToList(stationList, StationQueryVO.class);
        PageVO<StationQueryVO> pageVO = new PageVO<>();
        pageVO.setList(lists);
        pageVO.setTotal(pageInfo.getTotal());

        return pageVO;
    }

    public void deleteStation(Long id) {
        stationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询全部的车站信息 根据name_pinyin进行升序排列
     * @return
     */
    public List<StationQueryVO> QueryTrainAll(){
        StationExample stationExample = new StationExample();
        stationExample.setOrderByClause("name_pinyin asc");
        List<Station> stationList = stationMapper.selectByExample(stationExample);
        return BeanUtil.copyToList(stationList, StationQueryVO.class);
    }
}
